import { axios } from '@/utils/request'
import Vue from 'vue'
import { ACCESS_TOKEN } from "@/store/mutation-types"

const api = {
  user: '/online/user',
  role: '/online/role',
  service: '/online/service',
  permission: '/online/permission',
  permissionNoPager: '/online/permission/no-pager'
}

export default api

export function postTokenAction(url,parameter) {
  const token = Vue.ls.get(ACCESS_TOKEN)
  return axios({
    url: url,
    method:'post' ,
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
      "X-Access-Token": token
    },
    data: parameter
  })
}

//post
export function postAction(url,parameter) {
  return axios({
    url: url,
    method:'post' ,
    data: parameter
  })
}

//post method= {post | put}
export function httpAction(url,parameter,method) {
  return axios({
    url: url,
    method:'post' ,
    data: parameter
  })
}

//put
export function putAction(url,parameter) {
  return axios({
    url: url,
    method:'post',
    data: parameter
  })
}

//get
export function getAction(url,parameter) {
  return axios({
    url: url,
    method: 'post',
    params: parameter
  })
}

//deleteAction
export function deleteAction(url,parameter) {
  return axios({
    url: url,
    method: 'post',
    params: parameter
  })
}

export function getUserList(parameter) {
  return axios({
    url: api.user,
    method: 'post',
    params: parameter
  })
}

export function getRoleList(parameter) {
  return axios({
    url: api.role,
    method: 'post',
    params: parameter
  })
}

export function getServiceList(parameter) {
  return axios({
    url: api.service,
    method: 'post',
    params: parameter
  })
}

export function getPermissions(parameter) {
  return axios({
    url: api.permissionNoPager,
    method: 'post',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService(parameter) {
  return axios({
    url: api.service,
    method: parameter.id == 0 ? 'post' : 'put',
    data: parameter
  })
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url,parameter){
  return axios({
    url: url,
    params: parameter,
    method:'post' ,
    responseType: 'blob'
  })
}
export function downXmlFile(url,parameter){
  return axios({
    url: url,
    params: parameter,
    method:'post' ,
    responseType: 'text'
  })
}
/**
 * 获取文件访问路径
 * @param avatar
 * @param imgerver
 * @param str
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar,imgerver,subStr) {
  if(avatar && avatar.indexOf(subStr) != -1 ){
    return avatar;
  }else{
    return imgerver + "/" + avatar;
  }
}
